package org.southyin;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.southyin.mapper.CategoryMapper;
import org.southyin.mapper.EmpMapper;
import org.southyin.mapper.OrderMapper;
import org.southyin.mapper.UserMapper;
import org.southyin.model.Category;
import org.southyin.model.Emp;
import org.southyin.model.Order;
import org.southyin.model.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//        Emp emp = new Emp();
//        emp.setEname("tom");
//        emp.setJob("clerk");

//        List<Emp> emps = mapper.getEmpsByEnameOrJob2(emp);
//        System.out.println(emps);
//
//        List<Emp> list = mapper.getEmpsByEname("l");
//        System.out.println(list);

//        int[] ids = {2, 4, 6};
//        List<Emp> list2 = mapper.getEmpsByIds(ids);
//        System.out.println(list2);

//        Emp emp = new Emp();
//        emp.setEname("zhangsan");
//        emp.setJob("clerk");
//        int i = mapper.addEmp(emp);

//        mapper.delEmpById(12);

//        int[] ids = {2, 4, 6};
//        mapper.multiDelEmpByIds(ids);

//        Emp emp = new Emp();
//        emp.setEname("AAA");
//        emp.setId(1L);
//        mapper.updateEmp(emp);

//        Emp emp = new Emp();
//        emp.setJob("salesman");
//        List<Emp> emps = mapper.listEmps(emp);
//        System.out.println(emps);

//        Emp emp = new Emp();
//        emp.setId(1L);
//        emp.setEname("tom");
//        emp.setJob("test");
//        mapper.updateEmp2(emp);

//        Emp emp = new Emp();
//        emp.setEname("tom123");
//        emp.setJob("test");
//        System.out.println(emp.getId());
//        int i = mapper.addEmp2(emp);
//        sqlSession.commit();
//        System.out.println(emp.getId());

//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        List<User> users = mapper.listUserOrders();
//        System.out.println(users);

//        List<User> users1 = mapper.listUserOrdersPros();
//        System.out.println(users1);

//        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
//        Order order1 = mapper.listOrderUser(9001);
//        Order order2 = mapper.listOrderUser(9001);

        // Mybatis 一级缓存 ：默认开启的
        // 执行过的SQL会被缓存，再次执行，如果缓存中有，直接命中执行，不再进行解析，能提高效率
        // Mybatis 二级缓存 ：需要手动开启

        CategoryMapper mapper = sqlSession.getMapper(CategoryMapper.class);
        List<Category> categories = mapper.listCategory(-1);
        System.out.println(new ObjectMapper().writeValueAsString(categories));
    }
}
